package common;

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import io.restassured.response.Response;
import org.apache.commons.configuration.ConfigurationException;
import org.testng.annotations.*;
import utils.GetFileMess;
import io.restassured.RestAssured;
import utils.WritePropertiesUtil;

import static utils.WritePropertiesUtil.writePropertiesFile;

/*
 *父类的注解可以被子类继承，所有的环境变量在父类进行配置
 *根据testng.xml文件传入的参数来选择环境参数，默认为yg_env
 */
public class SetUpTearDown1 {

//    @BeforeSuite
//    public void dataSetUp() throws SQLException, IOException, ClassNotFoundException {
//        ExcutSqlFile.excute("setUpSQL"); //案例执行前参数维护
//    }

    //环境配置
    @BeforeClass
    public  Data envSetUp() {
        JSONObject json= new JSONObject();
        String system = "env.properties";
        try {
            RestAssured.baseURI = new GetFileMess().getValue("baseURI", system);
            RestAssured.basePath = new GetFileMess().getValue("basePath", system);
            RestAssured.port = Integer.parseInt(new GetFileMess().getValue("port", system));
//            System.out.println(RestAssured.basePath);
            String userName=new GetFileMess().getValue("userName", system);
            String passwd=new GetFileMess().getValue("passwd", system);
            String orgId=new GetFileMess().getValue("orgId", system);
            json.put("userName", userName);
            json.put("passwd", passwd);
            json.put("orgId", orgId);
        } catch ( ConfigurationException e) {
            e.printStackTrace();
        }
        Response response= RunCaseJson.runCase(json,"post","api-auth/login");
        String jsonStr= response.asString();
        System.out.println(jsonStr);
//        JSONObject jsonObj=JSONObject.parseObject(jsonStr);
        Loginobject loginobject= JSONObject.parseObject(jsonStr,new TypeReference<Loginobject>(){}.getType());
        Data data= loginobject.getData();
        return data;
    }


//    public void beforeClass(int sheetIndex,String timeSlice,String medCode) {
//        this.sheetIndex = sheetIndex;
//        this.timeSlice=timeSlice;
//        this.medCode=medCode;
//    }

    /*
     *创建environment.properties并放到allure-results目录下，测试报告展现
     */
    @AfterSuite
    public void createEnvPropertiesForReport() throws IOException {
/*        Map<String, String> data = new HashMap<>();
        String database = "iiaccount_db.properties";
        data.put("DatabaseLoginName", new GetFileMess().getValue("DB_Name", database));
        data.put("DatabaseLoginPass", new GetFileMess().getValue("DB_Password", database));
        data.put("DatabaseLoginIP", new GetFileMess().getValue("DB_IP", database));
        data.put("baseURI", RestAssured.baseURI + ":" + RestAssured.port + "/" + RestAssured.basePath);
        writePropertiesFile(data);
        */
        WritePropertiesUtil copyReportResources=new WritePropertiesUtil();
        copyReportResources.copyResources();
    }

//    @AfterSuite
//    public void dataTearDown() throws SQLException, IOException, ClassNotFoundException {
//        //案例执行结束后，对数据池的数据进行清理（删除或更新状态）
//        ExcutSqlFile.excute("tearDownSQL"); //案例执行后数据清理
//    }

    public static void main (String[] args){
//        envSetUp();
    }
}